package neusoft.example.com.yuejian06.Fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import neusoft.example.com.yuejian06.Adapter.ClubListAdapter;
import neusoft.example.com.yuejian06.Bean.Club;
import neusoft.example.com.yuejian06.Model.ClubListModel;
import neusoft.example.com.yuejian06.R;
import neusoft.example.com.yuejian06.RetrofitListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClubListFragment extends Fragment {
    private RecyclerView recyclerView;
    private List<Club> list;
    private LinearLayoutManager linearLayoutManager;
    private RetrofitListener<List<Club>> listener;
    private ClubListModel model;
    private ClubListAdapter adapter;

    private SwipeRefreshLayout swipeRefreshLayout;



    public ClubListFragment() {
        // Required empty public constructor
    }


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_clublist, container, false);
        listener();
        adapter=new ClubListAdapter(getActivity(),list);
        recyclerView=(RecyclerView) view.findViewById(R.id.club_rec);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new MyDividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));

        swipeRefreshLayout=(SwipeRefreshLayout) view.findViewById(R.id.club_list);
        swipeRefreshLayout.setColorSchemeColors(R.color.colorPrimary);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            Thread.sleep(1000);
                        }catch (InterruptedException e){
                            e.printStackTrace();
                        }
                        getActivity().runOnUiThread(new Runnable(){
                            @Override
                            public void run(){
                                if(list != null){
                                    list.clear();
                                }
                                model.getClubList(listener);
                                adapter.notifyDataSetChanged();
                                swipeRefreshLayout.setRefreshing(false);
                            }
                        });
                    }
                }).start();
            }
        });

        model=new ClubListModel();
        model.getClubList(listener);
        return view;
    }
    public void listener(){
        listener=new RetrofitListener<List<Club>>() {
            @Override
            public void onResponse(List<Club> l) {
                adapter.setList(l);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
        };
    }


}
